package com.ev.apis.controller.custom;


import com.ev.framework.annotation.EvApiByToken;
import com.ev.framework.utils.R;
import com.ev.hr.domain.JobDO;
import com.ev.hr.service.JobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  定时任务前端控制器
 * </p>
 *
 * @author 911435330@qq.com
 * @since 2019-09-05
 */
@RestController
@Api(value = "/",tags = "定时任务前端控制器")
@Slf4j
public class TQuartzJobController {

    @Autowired
    private JobService jobService;

    @Autowired
    private Scheduler myScheduler;

    @EvApiByToken(value = "/apis/job/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加定时任务")
    public R addJob(@Valid @RequestBody @ApiParam("保存定时任务") JobDO job){

/*        List<JobDO> list = jobService.findByJobClassName(job.getJobClassName());
        if(list!=null&&list.size()>0){
            return new R<Object>().setErrorMsg("该定时任务类名已存在");
        }*/
        jobService.save(job);
        return R.ok("创建定时任务成功");
    }

    @EvApiByToken(value = "/apis/job/edit", method = RequestMethod.POST)
    @ApiOperation(value = "更新定时任务")
    public R editJob(@ModelAttribute JobDO job){
        jobService.update(job);
        return R.ok("更新定时任务成功");
    }

    @EvApiByToken(value = "/apis/job/pause", method = RequestMethod.POST)
    @ApiOperation(value = "暂停定时任务")
    public R pauseJob(@ApiParam(value = "主键",required = true) @RequestParam(value="id",defaultValue = "") Long id){
        jobService.pauseJob(id);
        return R.ok("暂停定时任务成功");
    }

    @EvApiByToken(value = "/apis/job/resume", method = RequestMethod.POST)
    @ApiOperation(value = "恢复定时任务")
    public R resumeJob(@ApiParam(value = "主键",required = true) @RequestParam(value="id",defaultValue = "") Long id){
        jobService.resumeJob(id);
        return R.ok("恢复定时任务成功");
    }

}

